package org.cloud.Controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.cloud.bo.UserBo;
import org.cloud.pojo.Users;
import org.cloud.service.UserService;
import org.cloud.utils.CookieUtils;
import org.cloud.utils.IMOOCJSONResult;
import org.cloud.utils.JsonUtils;
import org.cloud.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("passport")
@Api(value = "注册登录",tags = {"用于注册登录的相关接口"})
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户名是否存在",notes = "用户名是否存在",httpMethod = "GET")
    @GetMapping("/checkUserIsExist")
    public IMOOCJSONResult checkUserIsExist(@RequestParam String username){
        boolean blank = StringUtils.isBlank(username);
        // 判断用户名不能为空
        if (blank) return IMOOCJSONResult.errorMsg("用户名不能为空！");

        boolean isExist = userService.CheckUserIsExist(username);
        if (isExist) {
            return IMOOCJSONResult.errorMsg("该账户已存在！");
        }

        return IMOOCJSONResult.ok();
    }

    @ApiOperation(value = "用户注册",notes = "用户注册",httpMethod = "POST")
    @PostMapping("/regist")
    public IMOOCJSONResult regist(@RequestBody UserBo userBo,
                                  HttpServletRequest request,
                                  HttpServletResponse response) {

        String username = userBo.getUsername();
        String password = userBo.getPassword();
        String confirmPassword = userBo.getConfirmPassword();

        //0.判断用户名和密码是否为空
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password) || StringUtils.isBlank(confirmPassword)) {
            return IMOOCJSONResult.errorMsg("用户名、密码或确认密码不能为空！");
        }

        //1.查询用户名是否存在
        boolean isExist = userService.CheckUserIsExist(username);
        if (isExist) {
            return IMOOCJSONResult.errorMsg("该账户已存在！");
        }

        //2.密码长度不能少于6位
        if (password.length() < 6) {
            return IMOOCJSONResult.errorMsg("密码长度不能少于6位！");
        }

        //3.判断两次密码是否一致
        if (!password.equals(confirmPassword)) {
            return IMOOCJSONResult.errorMsg("两次密码不一致！");
        }

        //4.实现注册
        Users user = userService.createUser(userBo);

        setNullProperty(user);

        // 设置cookie
        CookieUtils.setCookie(request,response,"user", JsonUtils.objectToJson(user),true);

        return IMOOCJSONResult.ok(user);
    }

    @ApiOperation(value = "用户登录",notes = "用户登录",httpMethod = "GET")
    @PostMapping("/login")
    public IMOOCJSONResult login(@RequestBody UserBo userBo,
                                 HttpServletRequest request,
                                 HttpServletResponse response) {

        String username = userBo.getUsername();
        String password = userBo.getPassword();

        //0.判断用户名和密码是否为空
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            return IMOOCJSONResult.errorMsg("用户名、密码不能为空！");
        }

        //4.实现登录
        Users user = null;
        try {
            user = userService.queryUserLogin(username, MD5Utils.getMD5Str(password));
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (user == null) {
            return IMOOCJSONResult.errorMsg("用户名、密码不正确！");
        }

        setNullProperty(user);

        // 设置cookie
        CookieUtils.setCookie(request,response,"user", JsonUtils.objectToJson(user),true);

        return IMOOCJSONResult.ok(user);
    }

    @ApiOperation(value = "用户退出登录",notes = "用户退出登录",httpMethod = "POST")
    @PostMapping("/logout")
    public IMOOCJSONResult logout(@RequestParam String userId,
                                  HttpServletRequest request,
                                  HttpServletResponse response) {
        CookieUtils.deleteCookie(request,response,"user");

        // TODO 用户退出登录，需要清空购物车
        // TODO 分布式会话中，需要清除用户数据

        return IMOOCJSONResult.ok();
    }

    public Users setNullProperty(Users users) {
        users.setPassword(null);
        users.setMobile(null);
        users.setEmail(null);
        users.setCreatedTime(null);
        users.setUpdatedTime(null);
        users.setBirthday(null);

        return users;
    }
}
